package com.home.cop.sysinfo.controller;

import com.home.cop.base.BaseController;
import com.home.cop.sysinfo.model.Institute;
import com.home.cop.sysinfo.model.InstituteRelation;
import com.home.cop.sysinfo.model.UserSession;
import com.home.cop.sysinfo.service.InstituteRelationService;
import com.home.cop.sysinfo.service.InstituteService;
import com.home.cop.employee.service.SysUserService;
import com.home.cop.system.RequiredAuth;
import com.home.cop.system.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api("机构相关")
@RequestMapping("/ins")
public class InstituteController extends BaseController {

    @Autowired
    private InstituteService instituteService;

    @Autowired
    private InstituteRelationService relationService;

    @Autowired
    private SysUserService userService;

    @PostMapping("/list")
    @ApiOperation(notes = "获取机构列表",value = "根据条件查询机构列表")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "instName", value = "机构名字", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "director", value = "负责人", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "broker", value = "对接人", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "instStatus", value = "状态：", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "busiType", value = "业务涵盖", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "regionId", value = "区域（区编号）", required = false, dataType = "String", paramType = "query")
            })
    public Result list(@RequestParam(value = "instName",required = false) String instName,
                       @RequestParam(value = "director",required = false) String director,
                       @RequestParam(value = "broker",required = false) String broker,
                       @RequestParam(value = "instStatus",required = false) String instStatus,
                       @RequestParam(value = "busiType",required = false) String busiType,
                       @RequestParam(value = "regionId",required = false) String regionId
    ){
        UserSession userSession = getUserSession();
        Integer instId = userSession.getInstId();
        Map<String,Object> params = new HashMap<>();
        params.put("instId",instId);
        if(StringUtils.isNotBlank(instName)){
            params.put("instName",instName);
        }
        if(StringUtils.isNotBlank(director)){
            params.put("director",director);
        }
        if(StringUtils.isNotBlank(broker)){
            params.put("broker",broker);
        }
        if(StringUtils.isNotBlank(instStatus)){
            params.put("instStatus",instStatus);
        }
        if(StringUtils.isNotBlank(busiType)){
            String[] busiTypeList = busiType.split(",") ;
            params.put("busiType",busiTypeList);
        }
        if(StringUtils.isNotBlank(regionId)){
            params.put("regionId",regionId);
        }
        return Result.returnSuccess().setData(instituteService.list(params,getPageSize(),getPageNum()));
    }

    @PostMapping("/add")
    @ApiOperation(notes = "新增机构信息（json提交）",value = "新增机构信息")
    @RequiredAuth
    public Result add(@RequestBody Institute institute){
        instituteService.insert(institute);
        return Result.returnSuccess();
    }

    @PostMapping("/instRelation")
    @ApiOperation(notes = "获取机构关系列表",value = "根据条件查询机构关系列表")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "instId", value = "机构id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result instRelation(@RequestParam(value = "instId") Integer instId){
        return Result.returnSuccess().setData(relationService.list(instId));
    }


    @PostMapping("/instRelationModify")
    @ApiOperation(notes = "机构关系列表维护",value = "机构关系列表维护")
    @RequiredAuth
    public Result instRelationModify(@RequestBody List<InstituteRelation> relations){
        relationService.modifyRelation(relations,relations.get(0).getInst1Id());
        return Result.returnSuccess();
    }

    @PostMapping("/deleteInstRelation")
    @ApiOperation(notes = "删除机构关系",value = "删除机构关系")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "instId", value = "机构id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result deleteInstRelation(@RequestParam(value = "instId") Integer instId){
        relationService.deleteInstRelation(instId);
        return Result.returnSuccess();
    }

    @PostMapping("/status")
    @ApiOperation(notes = "修改机构状态",value = "修改机构状态")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "instId", value = "机构id", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "机构状态，数据字典中查询", required = true, dataType = "String", paramType = "query")
    })
    public Result status(@RequestParam("instId") Integer instId,@RequestParam("status") String status){
        instituteService.changeStatus(instId,status);
        return Result.returnSuccess().setMessage("修改成功！");
    }

    @PostMapping("/delete")
    @ApiOperation(notes = "删除机构",value = "删除机构")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "instId", value = "机构id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result status(@RequestParam("instId") Integer instId){
        instituteService.delInstitute(instId);
        return Result.returnSuccess().setMessage("删除成功！");
    }

    @PostMapping("/listAll")
    @ApiOperation(notes = "获取所有机构列表",value = "获取所有机构列表,只返回机构名字和id")
    @RequiredAuth
    public Result listAllInst(){
        return Result.returnSuccess().setData(instituteService.listAll());
    }

    @PostMapping("/users")
    @ApiOperation(notes = "获取当前机构下的系统用户",value = "获取当前机构下的系统用户")
    @RequiredAuth
    @ApiImplicitParams({ @ApiImplicitParam(name = "instId", value = "机构id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result getInstUsers(@RequestParam("instId") Integer instId){
        return Result.returnSuccess().setData(userService.getInstUsers(instId));
    }
}
